<template>
  <div class="redis-task-monitor-container" v-loading="pageLoading">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-background">
        <div class="bg-pattern"></div>
        <div class="bg-gradient"></div>
      </div>
      
      <div class="page-title">
        <div class="title-icon">
          <div class="icon-container">
            <el-icon size="20"><Monitor /></el-icon>
            <div class="icon-glow"></div>
          </div>
        </div>
        <div class="title-content">
          <div class="title-main">
            <h1>Redis监控系统</h1>
            <div class="title-badge">
              <el-icon><Star /></el-icon>
              <span>REDIS MONITOR</span>
            </div>
          </div>
          <p class="page-description">
            <el-icon><DataAnalysis /></el-icon>
            Redis任务监控和队列监控管理平台
          </p>
          <div class="title-meta">
            <div class="meta-item">
              <el-icon><Setting /></el-icon>
              <span>环境配置</span>
            </div>
            <div class="meta-item">
              <el-icon><View /></el-icon>
              <span>任务监控</span>
            </div>
            <div class="meta-item">
              <el-icon><Connection /></el-icon>
              <span>队列监控</span>
            </div>
            <div class="meta-item">
              <el-icon><Bell /></el-icon>
              <span>告警管理</span>
            </div>
          </div>
        </div>
      </div>
      
      <div class="page-stats">
        <div class="stat-card">
          <div class="stat-icon">
            <el-icon><Setting /></el-icon>
          </div>
          <div class="stat-content">
            <div class="stat-number">{{ stats.total_envs || 0 }}</div>
            <div class="stat-label">环境数量</div>
            <div class="stat-trend">
              <el-icon><Menu /></el-icon>
              <span>全部</span>
            </div>
          </div>
        </div>
        
        <div class="stat-card success">
          <div class="stat-icon">
            <el-icon><View /></el-icon>
          </div>
          <div class="stat-content">
            <div class="stat-number">{{ stats.monitored_tasks || 0 }}</div>
            <div class="stat-label">监控任务</div>
            <div class="stat-trend">
              <el-icon><Check /></el-icon>
              <span>运行中</span>
            </div>
          </div>
        </div>
        
        <div class="stat-card info">
          <div class="stat-icon">
            <el-icon><Connection /></el-icon>
          </div>
          <div class="stat-content">
            <div class="stat-number">{{ stats.monitored_queues || 0 }}</div>
            <div class="stat-label">监控队列</div>
            <div class="stat-trend">
              <el-icon><TrendCharts /></el-icon>
              <span>活跃</span>
            </div>
          </div>
        </div>
        
        <div class="stat-card warning">
          <div class="stat-icon">
            <el-icon><Bell /></el-icon>
          </div>
          <div class="stat-content">
            <div class="stat-number">{{ stats.alerting_count || 0 }}</div>
            <div class="stat-label">告警中</div>
            <div class="stat-trend">
              <el-icon><Warning /></el-icon>
              <span>需关注</span>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- Tab 标签页 -->
    <el-tabs v-model="activeTab" class="monitor-tabs" @tab-change="handleTabChange">
      <el-tab-pane label="任务数据" name="task-data">
        <TaskDataTab ref="taskDataRef" />
      </el-tab-pane>
      
      <el-tab-pane label="队列统计数据" name="queue-stats">
        <QueueStatsTab ref="queueStatsRef" />
      </el-tab-pane>
      
      <el-tab-pane label="爬虫状态" name="spider-status">
        <SpiderStatusTab ref="spiderStatusRef" />
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { 
  Monitor, Star, DataAnalysis, Setting, View, Connection, Bell,
  Menu, Check, TrendCharts, Warning
} from '@element-plus/icons-vue'
import TaskDataTab from './components/TaskDataTab.vue'
import QueueStatsTab from './components/QueueStatsTab.vue'
import SpiderStatusTab from './components/SpiderStatusTab.vue'
import { useRedisTaskMonitorStore } from '@/stores/redisTaskMonitor'
import { createLogger } from '@/utils/logger'

const componentLogger = createLogger('RedisTaskMonitor')

const store = useRedisTaskMonitorStore()
const pageLoading = computed(() =>
  store.taskDataLoading ||
  store.queueStatsLoading ||
  store.spiderStatusLoading ||
  store.taskConfigsLoading ||
  store.queueConfigsLoading
)

const activeTab = ref('task-data')
const taskConfigRef = ref(null)
const taskDataRef = ref(null)
const queueStatsRef = ref(null)
const spiderStatusRef = ref(null)

const stats = ref({
  total_envs: 0,
  monitored_tasks: 0,
  monitored_queues: 0,
  alerting_count: 0
})

// 获取统计信息
const fetchStats = async () => {
  try {
    // 获取任务配置统计
    await store.fetchTaskConfigs({ page: 1, pageSize: 1 })
    stats.value.monitored_tasks = store.taskConfigsStats.monitored_count || 0

    // 获取队列配置统计
    await store.fetchQueueConfigs({ page: 1, pageSize: 1 })
    stats.value.monitored_queues = store.queueConfigsStats.monitored_count || 0

    // 计算告警数量
    const taskAlerting = store.taskConfigsStats.alerting_count || 0
    const queueAlerting = store.queueConfigsStats.alerting_count || 0
    stats.value.alerting_count = taskAlerting + queueAlerting

    componentLogger.info('统计数据加载成功', stats.value)
  } catch (error) {
    componentLogger.error('加载统计数据失败', error)
  }
}

// Tab 切换处理
const handleTabChange = (tabName) => {
  componentLogger.info('切换Tab', { tab: tabName })
  
  // 根据Tab切换加载对应的数据
  switch (tabName) {
    case 'task-data':
      if (taskDataRef.value && taskDataRef.value.fetchData) {
        taskDataRef.value.fetchData()
      }
      break
    case 'queue-stats':
      if (queueStatsRef.value && queueStatsRef.value.fetchData) {
        queueStatsRef.value.fetchData()
      }
      break
    case 'spider-status':
      if (spiderStatusRef.value && spiderStatusRef.value.fetchData) {
        spiderStatusRef.value.fetchData()
      }
      break
  }
}

onMounted(() => {
  fetchStats()
  componentLogger.info('Redis监控系统页面加载完成')
})
</script>

<style scoped>
.redis-task-monitor-container {
  padding: 0;
  background: var(--el-bg-color-page);
  min-height: 100vh;
}

/* 页面头部 */
.page-header {
  position: relative;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  padding: 20px 24px;
  border-radius: 12px;
  overflow: hidden;
  color: #303133;
  min-height: 120px;
  border: 1px solid #e4e7ed;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.header-background {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  opacity: 0.05;
  pointer-events: none;
}

.bg-pattern {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image: 
    radial-gradient(circle at 20% 80%, rgba(220, 38, 38, 0.05) 0%, transparent 50%),
    radial-gradient(circle at 80% 20%, rgba(239, 68, 68, 0.05) 0%, transparent 50%);
  animation: patternMove 30s ease-in-out infinite;
}

.bg-gradient {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
}

.page-title {
  display: flex;
  gap: 16px;
  align-items: center;
  flex: 1;
  position: relative;
  z-index: 1;
}

.title-icon .icon-container {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  background: linear-gradient(135deg, #dc2626 0%, #ef4444 100%);
  border-radius: 8px;
  color: white;
  box-shadow: 0 2px 8px rgba(220, 38, 38, 0.3);
  transition: all 0.3s ease;
}

.title-icon .icon-container:hover {
  transform: scale(1.05);
  box-shadow: 0 4px 16px rgba(220, 38, 38, 0.4);
}

.icon-glow {
  position: absolute;
  top: -4px;
  left: -4px;
  right: -4px;
  bottom: -4px;
  background: linear-gradient(45deg, rgba(220, 38, 38, 0.2), rgba(239, 68, 68, 0.3));
  border-radius: 12px;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.title-icon .icon-container:hover .icon-glow {
  opacity: 1;
}

.title-content {
  flex: 1;
}

.title-main {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.title-main h1 {
  margin: 0;
  font-size: 24px;
  font-weight: 600;
  color: #303133;
  letter-spacing: -0.3px;
}

.title-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 12px;
  background: linear-gradient(135deg, #dc2626 0%, #ef4444 100%);
  border-radius: 12px;
  font-size: 11px;
  font-weight: 600;
  color: white;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  box-shadow: 0 2px 8px rgba(220, 38, 38, 0.25);
}

.page-description {
  margin: 0 0 12px 0;
  color: #606266;
  font-size: 14px;
  font-weight: 400;
  line-height: 1.5;
  display: flex;
  align-items: center;
  gap: 8px;
}

.page-description .el-icon {
  font-size: 16px;
  color: #909399;
}

.title-meta {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 6px;
  color: #909399;
  font-size: 12px;
  font-weight: 500;
}

.meta-item .el-icon {
  font-size: 14px;
}

/* 统计卡片 */
.page-stats {
  display: flex;
  gap: 20px;
  position: relative;
  z-index: 1;
  flex-wrap: wrap;
}

.stat-card {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 8px;
  padding: 16px 20px;
  display: flex;
  align-items: center;
  gap: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
  min-width: 140px;
}

.stat-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
}

.stat-icon {
  width: 32px;
  height: 32px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f0f2f5;
  color: #606266;
  flex-shrink: 0;
}

.stat-card.success .stat-icon {
  background: #f0f9ff;
  color: #67c23a;
}

.stat-card.info .stat-icon {
  background: #f4f4f5;
  color: #909399;
}

.stat-card.warning .stat-icon {
  background: #fef0f0;
  color: #f56c6c;
}

.stat-content {
  flex: 1;
  min-width: 0;
}

.stat-number {
  font-size: 20px;
  font-weight: 600;
  color: #303133;
  line-height: 1;
  margin-bottom: 4px;
}

.stat-label {
  font-size: 12px;
  color: #909399;
  line-height: 1;
  margin-bottom: 4px;
}

.stat-trend {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  color: #606266;
}

.stat-trend .el-icon {
  font-size: 12px;
}

/* Tab 样式 */
.monitor-tabs {
  background: white;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.monitor-tabs :deep(.el-tabs__item) {
  font-size: 16px;
  font-weight: 500;
}

.monitor-tabs :deep(.el-tabs__item.is-active) {
  color: #dc2626;
}

.monitor-tabs :deep(.el-tabs__active-bar) {
  background-color: #dc2626;
}

/* 动画效果 */
@keyframes patternMove {
  0%, 100% {
    transform: translateX(0) translateY(0);
  }
  25% {
    transform: translateX(-10px) translateY(-5px);
  }
  50% {
    transform: translateX(0) translateY(-10px);
  }
  75% {
    transform: translateX(10px) translateY(-5px);
  }
}
</style>

